package com.mvc.security.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.mvc.framework.service.BaseService;
import com.mvc.security.model.UserDepartment;
@Service
public class UserDepartmentManager  extends BaseService<UserDepartment, Integer>  {
	public List<UserDepartment> getUserDepartmentsByUserId(Integer userId){
		UserDepartment example = new UserDepartment();
		example.setUserId(userId);
		return getHibernateTemplate().findByExample(example);
	}

	public void saveUserDeapartments(Integer userId,List<UserDepartment> userDepartments) {
		deleteUserDepartmentByUserId(userId);
		for (UserDepartment userDepartment : userDepartments) {
			if(null != userDepartment.getDepartmentId()){
				userDepartment.setUserId(userId);
				save(userDepartment);
			}
		}
	}

	public void deleteUserDepartmentByUserId(Integer userId) {
		deleteByWhere("userId = " + userId);
	}
}
